export default {

	/* 前端接口域名 */
	BASE_URL: 'https://v6.thinkadmin.top',

	/* 返回对象实例 */
	make(vm) {
		return (this.vm = vm), this;
	},

	/* 接口自动提交 */
	post(url, data, tips, okTips, erTips) {
		let that = this;
		return new Promise((resolve, reject) => {
			if (tips !== false) {
				if (tips === true) tips = '提交中...';
				that.vm.$u.message.showLoading(tips);
			}
			that.vm.$u.post(url, data || {}).then(ret => {
				if (tips !== false) {
					that.vm.$u.message.hideLoading();
				}
				if (erTips === false) return reject(ret);
				if (okTips === false) return resolve(ret)
				that.auto(ret, okTips, erTips).then(() => resolve(ret), () => reject(ret));
			});
		});
	},

	/* 结果自动处理 */
	auto(ret, okTips, erTips) {
		return new Promise((resolve, reject) => {
			if (ret.code) {
				uni.showToast({
					mask: true,
					icon: 'none',
					title: typeof okTips === 'string' ? okTips : ret.info,
					duration: 2000
				});
				setTimeout(resolve, 2100);
			} else {
				uni.showToast({
					mask: true,
					icon: 'none',
					title: typeof erTips === 'string' ? erTips : ret.info,
					duration: 2000
				});
				setTimeout(reject, 2100);
			}
		});
	}
}
